import WebAPI from "./index.js"

let token = null
uni.getStorage({
	key: 'token',
	success: res => {
		token = res.data
	}
});

uni.getStorage({
	key: 'token',
	success: async res => {},
	fail: async res => {
		uni.login({
			provider: 'weixin',
			success: function(loginRes) {
				// 获取登录凭证
				var code = loginRes.code;
				uni.request({
					url: WebAPI.Login,
					method: 'POST',
					data: {
						jsCode: code,
						appId: "hdb01"
					},
					success: async function(res) {
						// 处理服务器端返回的数据
						uni.setStorage({
							key: 'token',
							data: res.data.data.token,
							success: function() {}
						});
						uni.setStorage({
							key: 'hasPhone',
							data: res.data.data.hasPhone,
							success: function() {}
						});
					},
					fail: function(err) {
						console.error('请求失败', err);
					}
				});
			},
			fail: function(error) {
				console.error('登录失败', error);
			}
		});


	}
});


let data = null

function request(url, method, data, token) {
	if (token == null) {
		try {
			const value = uni.getStorageSync('token');
			if (value) {
				token = value
			}
		} catch (e) {
			// error
		}

	}
	return new Promise((resolve, reject) => {
		uni.request({
			url,
			method,
			header: {
				Authorization: "Bearer:" + token // 修改这里的连接方式
			},
			data,
			success: res => {
				if (res.data.retCode != 0) {
					uni.showToast({
						icon: "none",
						title: res.data.retMsg,
						duration: 3000
					})
					if (res.data.retCode == 204) {
						setTimeout(() => {
							uni.navigateTo({
								url: "/pages/showGetPhone/showGetPhone"
							})
						}, 3000)
					}
				} else {
					resolve(res.data.data);
				}
			},
			fail: err => {
				console.error(err);
				reject(err);
			}
		});
	});
}

function request2(url, method, data, token) {
	if (token == null) {
		try {
			const value = uni.getStorageSync('token');
			if (value) {
				token = value
			}
		} catch (e) {
			// error
		}

	}
	return new Promise((resolve, reject) => {
		uni.request({
			url,
			method,
			header: {
				Authorization: "Bearer:" + token // 修改这里的连接方式
			},
			data,
			success: res => {
				resolve(res.data);
			},
			fail: err => {
				console.error(err);
				reject(err);
			}
		});
	});
}

// 文件上传
export function upLoadFile(data) {
	return request(WebAPI.upLoadFile, 'post', data, token)
}
// 获取用户信息
export function getUserDetails() {
	return request(WebAPI.getUserDetails, 'get', "", token)
}

// 更新用户信息
export function updataUserDetails(data) {
	return request(WebAPI.updataUserDetails, 'post', data, token)
}


//  获取用户手机号
export function getUserPhone(data) {
	return request(WebAPI.getUserPhone, 'post', data, token)
}

//  查询用户简要信息
export function getUserInfo(token) {
	return request(WebAPI.getUserInfo, 'get', data, token)
}

//  用户申请提现
export function userWithdrawal(data) {
	return request(WebAPI.userWithdrawal, 'post', data, token)
}

//  用户提现记录
export function userWithdrawalRecords(data) {
	return request(WebAPI.userWithdrawalRecords + data, 'get', null, token)
}

//  用户账户变动明细
export function userAccountChange(data) {
	return request(WebAPI.userAccountChange + data, 'get', null, token)
}

//  查询用户账户余额
export function userAccountBalance() {
	return request(WebAPI.userAccountBalance, 'get', data, token)
}

//  我的订单列表
export function myOrderList(num) {
	return request(WebAPI.myOrderList + num, 'get', data, token)
}

//  我参与过的活动数量
export function joinActiveNum() {
	return request(WebAPI.joinActiveNum, 'get', data, token)
}

//  订单人数统计
export function accountPeople(activityId) {
	return request(WebAPI.accountPeople + activityId, 'get', data, token)
}

//  分页查看报名人员名单
export function showJoinPeople(activityId, pageNo) {
	return request(WebAPI.showJoinPeople + activityId + "/" + pageNo, 'get', data, token)
}

//  活动报名
export function createOrder(data, token) {
	return request(WebAPI.createOrder, 'post', data, token)
}

//  活动详情
export function activeDetail(num) {
	return request(WebAPI.activeDetail + num, 'get', data, token)
}

//  活动动态列表
export function activeDetailList(activityId, pageNo) {
	return request(WebAPI.activeDetailList + activityId + "/" + pageNo, 'get', data, token)
}
// export function activeDetailList(data) {
// 	return request(WebAPI.activeDetailList, 'get', data, token)
// }
//  热门活动
export function hotActive() {
	return request(WebAPI.hotActive, 'get', null, token)
}

//  活动查询
export function getActive(data) {
	return request(WebAPI.getActive, 'get', data, token)
}

//  我发起的活动
export function myActive(data) {
	return request(WebAPI.myActive, 'post', data, token)
}

//  精彩活动回顾
export function showActive() {
	return request(WebAPI.showActive, 'get', null, token)
}

//  保存活动草稿
export function saveActive(data) {
	return request(WebAPI.saveActive, 'post', data, token)
}

//  发布动态
export function publishUpdates(data) {
	console.log(data, 'publishUpdates');
	return request(WebAPI.publishUpdates, 'post', data, token)
}

//  发布活动
export function releaseActivity(data) {
	return request(WebAPI.releaseActivity, 'post', data, token)
}

//  查询所有活动分类
export function activityType() {
	return request(WebAPI.activityType, 'get', data)
}


// 申请团长
export function applyingTeamLeader() {
	return request(WebAPI.applyingTeamLeader, 'get', data)
}

// 导出报名人员名单
export function exportApplicantsList(num) {
	return request2(WebAPI.exportApplicantsList + num, 'get', data)
}

// 查询用户邀请码
export function getUserCode(num) {
	return request2(WebAPI.getUserCode, 'get', data)
}

// 活动详情预览
export function activeDetailView(num) {
	return request(WebAPI.activeDetailView + num, 'get', data)
}